import request from '@/utils/request'
import { ApiMethodContants, type ListResultData, type Pagination, type ResultData } from './base'

export interface BlogResult {
  id: number
  blogContent: string
  enable: boolean
  createDate: string
  updateDate: string
  blogTitle: string
  coverUrl: string
  blogType: number
  editorType: 1 | 2
  blogDesc?: ''
}

export interface BlogTagsResult {
  id?: number
  tagName: string
  tagDesc: string
  tagImg: string
  enable: boolean
  createDate: string | undefined
  updateDate: string | undefined
}
export interface addBlogTagsDto {
  tagName: string
  tagDesc: string
  tagImg: string
  enable: boolean
  createDate: string | undefined
  updateDate: string | undefined
}
export interface updateBlogTagsDto {
  id: number
  tagName: string
  tagDesc: string
  tagImg: string
  enable: boolean
  createDate: string | undefined
  updateDate: string | undefined
}
export interface addOrUpdateBlogDto {
  blogContent: string
  editorType: 1 | 2
  tagList: number[]
  coverUrl: string
  blogType: number
  id?: number
  enable?: boolean
  blogTitle: string
  blogDesc?: string
}
export interface BlogTagsListItem extends BlogTagsResult {
  blogId: number
  tagId: number
  relationId: number
}

export interface BlogAndTagResult extends BlogResult {
  tagsList?: BlogTagsListItem[]
}
export interface ProjectResult {
  comment: string
  createDate: string
  name: string
  id: string
  updateDate: string
  url: string
  enable: boolean
  previewUrl: string
  [property: string]: any
}
export interface addProjectDto {
  comment?: string
  name: string
  url: string
  previewUrl?: string
  enable?: boolean
  [property: string]: any
}

export interface editProjectDto extends addProjectDto {
  id: string
}

interface GetBlogTagsDto {
  key?: string
  enable?: boolean
}

interface GetProjectListDto {
  key?: string
}

export function getBlogTags(getBlogTagsDto: GetBlogTagsDto): Promise<ResultData<BlogTagsResult[]>> {
  return request({
    url: `/blogs/getTags`,
    params: {
      ...(getBlogTagsDto || {})
    },
    method: ApiMethodContants.GET
  })
}

export function getProjectList(param: GetProjectListDto): Promise<ResultData<ProjectResult[]>> {
  return request({
    url: `/projects/getAll`,
    params: {
      ...(param || {})
    },
    method: ApiMethodContants.GET
  })
}

export function addProjectItem(data: addProjectDto): Promise<ResultData<null>> {
  return request({
    url: `/projects/add`,
    method: ApiMethodContants.POST,
    data
  })
}

export function editProjectItem(data: editProjectDto): Promise<ResultData<null>> {
  return request({
    url: `/projects/update`,
    method: ApiMethodContants.POST,
    data
  })
}

export function addOrUpdateTag(data: BlogTagsResult): Promise<ResultData<null>> {
  return request({
    url: `/blogs/saveTag`,
    method: ApiMethodContants.POST,
    data
  })
}

export function deleteTag(id: number): Promise<ResultData<null>> {
  return request({
    url: `/blogs/deleteTag`,
    method: ApiMethodContants.DELETE,
    params: {
      id
    }
  })
}
export interface QueryBlogParams extends Pagination {
  key: string
  blogType: number
}
// 文章内容
export function getBlogs(queryParams: QueryBlogParams): Promise<ResultData<ListResultData<BlogAndTagResult[]>>> {
  return request({
    url: `/blogs/getBlogs`,
    params: {
      ...queryParams
    },
    method: ApiMethodContants.GET
  })
}

export function getBlogContent(queryParams: { id: number }): Promise<ResultData<BlogResult>> {
  return request({
    url: `/blogs/getBlogContent`,
    params: {
      ...queryParams
    },
    method: ApiMethodContants.GET
  })
}

export function addOrUpdateBlogItem(data: addOrUpdateBlogDto): Promise<ResultData<null>> {
  return request({
    url: `/blogs/saveBlog`,
    method: ApiMethodContants.POST,
    data
  })
}

export function deleteBlogItem(id: number): Promise<ResultData<null>> {
  return request({
    url: `/blogs/deleteBlog`,
    method: ApiMethodContants.DELETE,
    params: {
      id
    }
  })
}
